using System;
using System.Collections;
using System.Linq;
using System.Xml.XPath;
using Comdiv.Application;
using Comdiv.Conversations;
using Comdiv.Extensions;
using Comdiv.Inversion;
using Comdiv.IO;
using Comdiv.Logging;
using Comdiv.Model;
using Comdiv.Model.Interfaces;
using Comdiv.Persistence;
using Comdiv.Security;
using Comdiv.Security.Acl;
using NUnit.Framework;

namespace Comdiv.Extensions.Tests{
    [TestFixture]
    public class BoolExtensions{
        private IEnumerable testIenum(){
            yield break;
        }

        [Test]
        [TestCase(null, false)]
        [TestCase(false, false)]
        [TestCase(true, true)]
        [TestCase(0, false)]
        [TestCase(1, true)]
        [TestCase(0.0, false)]
        [TestCase(2.0, true)]
        [TestCase(0.01, true)]
        [TestCase("", false)]
        [TestCase("aaa", true)]
        [TestCase("False", true)]
        [TestCase("0", true)]
        public void BaseTypeBooleanTest(object value, bool result){
            Assert.AreEqual(BooleanExtensions.yes(value), result);
            Assert.AreNotEqual(BooleanExtensions.no(value), result);
        }

        [Test]
        public void IEnumerableBool(){
            Assert.IsTrue(BooleanExtensions.yes(new[]{1, 2}));
            Assert.IsTrue(BooleanExtensions.yes(((object) new[]{1, 2})));
            Assert.IsFalse(BooleanExtensions.yes((new object[]{})));
            Func<IEnumerable> func = () => { return null; };
            Assert.IsFalse(BooleanExtensions.yes(func()));
            Assert.IsFalse(BooleanExtensions.yes(((object) func())));
            Func<IEnumerable> func2 = testIenum;
            Assert.IsFalse(BooleanExtensions.yes(func2()));
        }

        [Test]
        [TestCase(null, false)]
        [TestCase("", false)]
        [TestCase("  ", false)]
        [TestCase("\r\n ", false)]
        [TestCase("\t ", false)]
        [TestCase("False", false)]
        [TestCase("0", false)]
        [TestCase("1", true)]
        [TestCase("true", true)]
        [TestCase("True", true)]
        [TestCase("false", false)]
        [TestCase("f", false)]
        public void StringToBool(string value, bool result){
            Assert.AreEqual(value.toBool(), result);
            //  Assert.AreNotEqual(value.ToBoolean(), result);
        }

        [Test]
        public void XPathNodeIteratorBool(){
            Assert.IsFalse(BooleanExtensions.yes((null as XPathNodeIterator)));
            Assert.IsTrue(BooleanExtensions.yes("<xml><a/></xml>".asXPathNavigable().Select("//a")));
            Assert.IsFalse(BooleanExtensions.yes("<xml><b/></xml>".asXPathNavigable().Select("//a")));
        }
    }
}